using Avalonia.Controls;
using System;

namespace AvaloniaDrawingPad.Models.Commands
{
    /// <summary>
    /// 添加图形命令
    /// </summary>
    public class AddShapeCommand : ICommand
    {
        private readonly Layer _layer;
        private readonly Control _shape;
        private readonly SelectableShape _selectableShape;
        private readonly LayerManager _layerManager;
        private readonly Action<SelectableShape>? _onShapeAdded;
        
        public string Description => "添加图形";
        
        public AddShapeCommand(
            Layer layer, 
            Control shape, 
            SelectableShape selectableShape, 
            LayerManager layerManager,
            Action<SelectableShape>? onShapeAdded = null)
        {
            _layer = layer;
            _shape = shape;
            _selectableShape = selectableShape;
            _layerManager = layerManager;
            _onShapeAdded = onShapeAdded;
        }
        
        public void Execute()
        {
            // 添加图形到图层
            if (!_layer.Content.Children.Contains(_shape))
            {
                _layer.Content.Children.Add(_shape);
            }
            
            // 如果有回调，执行回调
            _onShapeAdded?.Invoke(_selectableShape);
            
            // 标记项目已修改
            _layerManager.MarkAsChanged();
        }
        
        public void Undo()
        {
            // 从图层中移除图形
            _layer.Content.Children.Remove(_shape);
            
            // 标记项目已修改
            _layerManager.MarkAsChanged();
        }
    }
} 